import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class utilLoading {
  static bool _isDialogShowing = false;

  /// 显示加载框（可以选择是否为 iOS 风格）
  static void show(BuildContext context, {bool isIOS = false, String? text}) {
    if (_isDialogShowing) return; // 防止重复弹出
    _isDialogShowing = true;

    showDialog(
      context: context,
      barrierDismissible: false, // 点击外部不关闭
      builder: (_) {
        return WillPopScope(
          onWillPop: () async => false, // 禁止返回键关闭
          child: Center(
            child: Container(
              width: 120,
              padding: EdgeInsets.all(20),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  isIOS
                      ? CupertinoActivityIndicator()
                      : CircularProgressIndicator(),
                  SizedBox(height: 16),
                  Text(text ?? '加载中...', style: TextStyle(fontSize: 16)),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  /// 手动关闭加载框
  static void dismiss(BuildContext context) {
    if (_isDialogShowing) {
      Navigator.of(context, rootNavigator: true).pop();
      _isDialogShowing = false;
    }
  }
}
